package com.outsource.framework.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.outsource.framework.domain.Tinfosection;
import com.outsource.framework.model.services.TinfosectionService;
import com.outsource.framework.util.common.Response;

@Controller
public class InfosectionContrller {

	@Autowired
	TinfosectionService service;
	
	@Autowired
	MessageSource messageSource;
	
	/**
	 * get apps list
	 * @param classifytype 0-news,2-app
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/WoWebAppClassify",method = {RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody Map<String, Object> WoWebAppClassify(
			@RequestParam("classifytype") String classifytype,
			HttpServletRequest request){
		String responseCode = getMessages("lable.code.success");
		String responseMessages = getMessages("lable.success");
		List<Object[]> temp = service.findByAllapps(classifytype);
		List<Tinfosection> responseEntry = new ArrayList<Tinfosection>();
		for (Iterator<Object[]> iterator = temp.iterator(); iterator.hasNext();) {
			Object[] object = (Object[]) iterator.next();
			Integer id = Integer.valueOf(object[0].toString());
			String name = String.valueOf(object[1]);
			String type = String.valueOf(object[2]);
			Tinfosection tf = new Tinfosection(id,name,type);
			responseEntry.add(tf);
		}
		Response response = new Response();
		Map<String, Object> responsemap = response.getResponse(responseCode, responseMessages, responseEntry);
		return responsemap;
	}
	
	/**
	 * get zh.properties content
	 * @param code
	 * @return
	 */
	public String getMessages(String code){
		return messageSource.getMessage(code, null,Locale.SIMPLIFIED_CHINESE);
	}
}
